package iss.anxios.controller;
import iss.anxios.dto.RespondentType;
import iss.anxios.service.RespondentService;
import iss.anxios.service.RespondentTypeService;
import iss.anxios.service.ServiceTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/respondenttypecontroller")
public class AttributeController {

	@Autowired
	private RespondentTypeService respondenttypeservice;
	@Autowired
	private RespondentService respondentservice;
	@Autowired
	private ServiceTest test;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView listAttributeItem() {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("respondenttypecontroller", respondenttypeservice.ListRespondentType());
		model.put("testcontroller",test.listServiceTest());
		model.put("data_from_request", "A dummy data");
		System.out.println("I am in get controller");
		return new ModelAndView("Attribute", model);
	}
	
	
	@RequestMapping(value="/olala", method = RequestMethod.POST)
	public ModelAndView saveMenuItem (ServletRequest request) {
		
		List<RespondentType> types = respondenttypeservice.ListRespondentType();
		List<Integer> checkedcheckbox = new ArrayList<Integer>();
		for(RespondentType type : types){
			if("on".equals(request.getParameter(type.getRespondenttypename()))){
				checkedcheckbox.add(type.getRespondenttypeid());
			}
		}
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("respondenttypecontroller", types);
		System.out.println("I reach....");
		model.put("testcontroller",test.listServiceTest());
		model.put("respondentcontroller",respondentservice.ListRespondent());
		model.put("selectedcheckboxes", checkedcheckbox);
		return new ModelAndView("Attribute", model);
	}
}
